package com.xiaoliang.controller;


import com.xiaoliang.entry.Page;
import com.xiaoliang.entry.Product;
import com.xiaoliang.entry.User;
import com.xiaoliang.service.impl.ProductServiceImpl;
import com.xiaoliang.service.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author XIAOLIANG
 * @ClassName ProductController.java
 * @Description TODO
 * @createTime 2021年03月02日 19:56:00
 */
@Controller
@RequestMapping("/product")
public class ProductController {

	ProductServiceImpl productService = new ProductServiceImpl();

	@RequestMapping("/queryList")
	public String productList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, String productName, String operation, String price, Map map){

		System.out.println("/queryList");
		Integer count = productService.getCountByCondition(productName,operation,price);
		Page page = new Page(pageSize, pageNum);
		page.setTotleElements(count);
		//获取 当前页数据
		List<Product> products = productService.searchByCondition(productName, operation, price, pageNum, pageSize);
		map.put("page",page);
		map.put("products",products);
		map.put("productName",productName);
		map.put("operation",operation);
		map.put("price",price);

//		Map<String, String[]> params = req.getParameterMap();    //获取最近一次 productlist.jsp页面 完整的请求地址 并将其存进session
		return "forward:/product/searchList.jsp";


	}

	@RequestMapping("/addOrUpdate")
	public String addProduct(HttpSession session, HttpServletRequest req){

		String type = req.getParameter("type");

		if ("add".equals(type)){                   //携带type=add 参数 表示添加的请求 需要清空session 中的product
			session.removeAttribute("product");
			session.setAttribute("type",2);

		}else {
			session.setAttribute("type",1);
		}

		return "redirect:/product/addOrUpdate.jsp";


	}

	@RequestMapping("/update")
	public String updateProduct(Product product,HttpSession session ){

		boolean b = productService.updateProduct(product);
		if (b){
			System.out.println("更新成功");
			return "redirect:/product/queryList";
		}else {

			session.setAttribute("msg","更新失败");
			return "redirect:/product/addOrUpdate.jsp";
		}

	}

	@RequestMapping("/delete/{id}")
	public String deleteById(@PathVariable("id") Integer id ){

		productService.deleteProduct(id);

		return "redirect:/product/queryList";


	}

	@RequestMapping("/getOneProduct/{id}")
	public String getOneProduct(HttpSession session,@PathVariable("id") Integer id){

		Product product = productService.selectOneById(id);
		session.setAttribute("product",product);
		session.setAttribute("type",1);
		session.setAttribute("requestAddr","update");
		return "/product/addOrUpdate.jsp";
	}

	@RequestMapping("/deleteChecked")
	public String deleteChecked(Integer[] ids){

		if (ids!=null){
			for ( Integer id : ids) {
				productService.deleteProduct(id);
				System.out.println("删除商品的ID"+id);
			}

		}

		return "redirect:/product/queryList";


	}


	@RequestMapping("/addProduct")
	public String addProduct(Product product,HttpSession session){

		System.out.println("add");
		System.out.println(product);
		boolean b = productService.addProduct(product);
		if(b){

			System.out.println("添加成功!");
			session.setAttribute("msg","添加成功!");

		}else {

			System.out.println("添加失败");
			session.setAttribute("msg","添加失败!");
		}
		return "redirect:/product/addOrUpdate.jsp";



	}



}
